<?php

	class feeds extends kleoplugins implements iplugins{
		function __construct(){
			$this->add_queue('feeds::check_feeds()',KLEO_PLUGINS_DIR.'/feeds/main.php','*','nopost');
			if(!$this->is_value('feeds_title')){
				$this->add_core_value('feeds_title', 'Blog posts');
			}
		}
		public function index($buff,&$buff){
			$this->add_form('feeds', 'Options', 'views/options.tpl', 'process_form_main');
		}

		function on_head(){
			return '<link rel="alternate" type="application/rss+xml" title="'.$this->get_core_value('feeds_title').'" href="'.$this->get_core_value('siteurl').'/plugins/feeds/output/blog.php" />';
			  
		}

		function on_body(){
		  
		}

		function on_ending(){

		}
		
		function plugin_install(){
			
		}
		function plugin_remove(){

		}
		function process_form_main(){
			if(!$this->is_value('feeds_limit')){
				$this->add_core_value('feeds_limit', (int)$_POST['feeds_limit']);
			} else {
				$this->set_core_value('feeds_limit', (int)$_POST['feeds_limit']);
			}
			if(!$this->is_value('feeds_title')){
				$this->add_core_value('feeds_title', $_POST['feeds_title']);
			} else {
				$this->set_core_value('feeds_title', $_POST['feeds_title']);
			}
			if(!$this->is_value('feeds_format')){
				$this->add_core_value('feeds_format', $_POST['feeds_format']);
			} else {
				$this->set_core_value('feeds_format', $_POST['feeds_format']);
			}
			$_SESSION['dashboard_status'] = array(0,'Feed options changed');
			$this->appreload();
		}
		function check_feeds(){
			if(!$this->is_value('feeds_format')){
				$format = 'rss';
			} else {
				$format = $this->get_core_value('feeds_format');
			}
			$limit = $this->get_core_value('feeds_limit');
			if(empty($limit)){
				$limit = 5;
			}
			$this->tpl_load(KLEO_PLUGINS_DIR.'feeds/views/rss.tpl',$feeds_buff);
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog WHERE status = 1 ORDER BY id DESC LIMIT $limit");
			$feed_items = null;
			while($row = $this->fetch_array($query)){
				unset($item);
				$this->tpl_load(KLEO_PLUGINS_DIR.'feeds/views/rss_entries.tpl',$item);
				$date = $this->stamp2array($row['posted']);
				$date = date(DATE_RSS, mktime($date['h'], $date['i'], $date['s'], $date['m'], $date['d'], $date['y']));
				$this->tpl_set(array(
						"FEED_TITLE" => $row['title'],
						"FEED_EXCERPT" => str_replace('&nbsp;','',strip_tags($row['content'])),
						"FEED_DATE" => $date,
						"FEED_LINK" => strip_tags(KLEO_SITEURL."/index.php?module=blog&p=".$row['id'])
					),false,$item,$item);
				$feed_items .= $item;
			}
			$this->tpl_load(KLEO_PLUGINS_DIR.'feeds/views/rss.tpl', $feed);
			$this->tpl_set(array(
					"RSS_ITEMS" => $feed_items
					),false,$feed,$feed);
			$this->compile($feed,$feed);
			if(file_exists(KLEO_PLUGINS_DIR.'feeds/output/blog.rss')){
				unlink(KLEO_PLUGINS_DIR.'feeds/output/blog.rss');
			}
			$output = fopen(KLEO_PLUGINS_DIR.'feeds/output/blog.rss', 'w');
			fwrite($output,$feed);
		}
  }


?>
